﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace NoteHelper.BaseControl
{
    public partial class CyDateBox : ComboBox
    {
        private class ToolStripMonthCalendar : ToolStripControlHost
        {
            public ToolStripMonthCalendar() : base(new MonthCalendar())
            {
                this.Margin = Padding.Empty;
                this.Padding = Padding.Empty;
                this.AutoSize = false;
            }

            public MonthCalendar MonthCalendarControl
            {
                get
                {
                    return Control as MonthCalendar;
                }
            }

            // Expose the AddBoldedDate method.
            public void AddBoldedDate(DateTime dateToBold)
            {
                MonthCalendarControl.AddBoldedDate(dateToBold);
            }

        }

        #region Class Data
        private const UInt32 WM_LBUTTONDOWN = 0x201;
        private const UInt32 WM_LBUTTONDBLCLK = 0x203;
        private const UInt32 WM_KEYF4 = 0x134;
        private const UInt32 WM_CTLCOLORLISTBOX = 0x0134;
        ToolStripMonthCalendar myTSMonthCalendar;
        ToolStripDropDown tsDD;
        #endregion

        public CyDateBox()
        {
            InitializeComponent();
            myTSMonthCalendar = new ToolStripMonthCalendar();
            tsDD = new ToolStripDropDown();

            // instantiere evenimente
            this.myTSMonthCalendar.MonthCalendarControl.DateSelected += this.myTSMonthCalendar_DateSelected;
            this.myTSMonthCalendar.MonthCalendarControl.KeyDown += this.myTSMonthCalendar_KeyDown;
        }

        #region Control's Methods
        private void myTSMonthCalendar_DateSelected(object sender, DateRangeEventArgs e)
        {
            this.Text = e.End.ToShortDateString();
            this.tsDD.Close();
        }
        private void myTSMonthCalendar_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                this.tsDD.Close();

        }
        #endregion

        #region Methods
        private Point CalculatePoz()
        {
            Point point = new Point(0, this.Height);

            if ((this.PointToScreen(new Point(0, 0)).Y + this.Height + this.myTSMonthCalendar.Height) > Screen.PrimaryScreen.WorkingArea.Height)
            {
                point.Y = -this.myTSMonthCalendar.Height - 7;
            }

            return point;
        }
        #endregion

        protected override void WndProc(ref Message m)
        {
            #region WM_KEYF4
            if (m.Msg == WM_KEYF4)
            {
                this.Focus();
                this.tsDD.Refresh();
                OpenDropDown();
                return;
            }
            #endregion

            #region WM_LBUTTONDBLCLK
            if (m.Msg == WM_LBUTTONDBLCLK || m.Msg == WM_LBUTTONDOWN)
            {
                OpenDropDown();
                return;
            }
            #endregion

            base.WndProc(ref m);
        }

        private void OpenDropDown()
        {
            if (!this.tsDD.Visible)
            {
                if (SeletedValue != null)
                    myTSMonthCalendar.MonthCalendarControl.SetDate(SeletedValue.Value);

                tsDD.Items.Add(this.myTSMonthCalendar);
                tsDD.Show(this, this.CalculatePoz());
            }
        }

        #region Property
        public DateTime? SeletedValue
        {
            get
            {
                string text = this.Text;
                int index = text.IndexOf("-");
                if (index != -1)
                    text = text.Substring(0, index);
                DateTime result;
                bool r = DateTime.TryParse(text, out result);
                if (r)
                    return result;

                return null;
            }
        }

        public override string Text
        {
            get { return base.Text; }
            set
            {
                string text = value;
                int index = text.IndexOf("-");
                if (index != -1)
                    text = text.Substring(0, index);
                DateTime result;
                bool r = DateTime.TryParse(text, out result);
                if (r)
                    base.Text = result.ToShortDateString();
                else
                    base.Text = value;
            }
        }
        #endregion
    }
}
